package cn.teacher.controller;

import cn.teacher.config.web.ResponseMessage;
import cn.teacher.config.web.ResponseStatusCode;
import cn.teacher.pojo.Teacher;
import cn.teacher.pojo.vo.JwtUser;
import cn.teacher.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 毛苏宇
 * User: me
 * Date: 2020/6/24
 * Time: 22:23
 * Description:
 */
@RestController
@RequestMapping("/info")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @PostMapping("/delete")
    public ResponseMessage delete(@RequestParam("userId") long userId) {
        final boolean remove = teacherService.remove(userId);
        if (remove) {
            return new ResponseMessage(ResponseStatusCode.CODE_SUCCESS, "删除成功", null);
        }
        return new ResponseMessage(ResponseStatusCode.CODE_FAIL, "删除失败", null);
    }

    @PostMapping("/getTeacherInfo")
    public ResponseMessage getTeacherInfo(JwtUser user) {
        return new ResponseMessage(ResponseStatusCode.CODE_SUCCESS, "查询成功", teacherService.find(user.getJwtId()));
    }

    @PostMapping("/save")
    public ResponseMessage save(Teacher teacher) {
        boolean insert = teacherService.add(teacher);
        if (insert) {
            return new ResponseMessage(ResponseStatusCode.CODE_SUCCESS, "添加成功", null);
        }
        return new ResponseMessage(ResponseStatusCode.CODE_FAIL, "资料失败", null);
    }

    @PostMapping("/update")
    public ResponseMessage update(Teacher teacher) {
        boolean modify = teacherService.modify(teacher);
        if (modify) {
            return new ResponseMessage(ResponseStatusCode.CODE_SUCCESS, "修改成功", null);
        }
        return new ResponseMessage(ResponseStatusCode.CODE_FAIL, "修改失败", null);
    }

    @PostMapping("/getTeacherInfo2")
    public ResponseMessage getTeacherInfo2(@RequestParam("userId") long userId) {
        return new ResponseMessage(ResponseStatusCode.CODE_SUCCESS, "查询成功", teacherService.find(userId));
    }

}
